# Copyright 2019 The IREE Authors
#
# Licensed under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

load("//build_tools/bazel:build_defs.oss.bzl", "iree_compiler_cc_library", "iree_gentbl_cc_library")

package(
    default_visibility = ["//visibility:public"],
    features = ["layering_check"],
    licenses = ["notice"],  # Apache 2.0
)

iree_gentbl_cc_library(
    name = "PassesIncGen",
    tbl_outs = [
        (
            ["--gen-pass-decls"],
            "Passes.h.inc",
        ),
    ],
    tblgen = "@llvm-project//mlir:mlir-tblgen",
    td_file = "Passes.td",
    deps = ["@llvm-project//mlir:PassBaseTdFiles"],
)

iree_compiler_cc_library(
    name = "PassHeaders",
    hdrs = [
        "PassDetail.h",
        "Passes.h",
        "Passes.h.inc",
    ],
    deps = [
        ":PassesIncGen",
        "//compiler/src/iree/compiler/Codegen/Dialect/Codegen/IR:IREECodegenDialect",
        "//compiler/src/iree/compiler/Dialect/HAL/IR",
        "//compiler/src/iree/compiler/Utils",
        "@llvm-project//mlir:LinalgTransforms",
        "@llvm-project//mlir:MemRefDialect",
        "@llvm-project//mlir:Pass",
        "@llvm-project//mlir:Transforms",
    ],
)

iree_gentbl_cc_library(
    name = "FoldTensorExtractOpIncGen",
    tbl_outs = [
        (
            ["--gen-rewriters"],
            "FoldTensorExtractOp.cpp.inc",
        ),
    ],
    tblgen = "@llvm-project//mlir:mlir-tblgen",
    td_file = "FoldTensorExtractOp.td",
    deps = [
        "@llvm-project//mlir:BufferizableOpInterfaceTdFiles",
        "@llvm-project//mlir:BufferizationOpsTdFiles",
        "@llvm-project//mlir:MemRefOpsTdFiles",
        "@llvm-project//mlir:OpBaseTdFiles",
        "@llvm-project//mlir:SubsetOpInterfaceTdFiles",
        "@llvm-project//mlir:TensorOpsTdFiles",
    ],
)

iree_compiler_cc_library(
    name = "Common",
    srcs = [
        "AddFastMathFlags.cpp",
        "BubbleUpOrdinalOps.cpp",
        "BufferizationAnalysis.cpp",
        "BufferizeCopyOnlyDispatchesPass.cpp",
        "CleanupBufferAllocViewPass.cpp",
        "ConcretizePadResultShape.cpp",
        "ConvertBf16ArithToF32.cpp",
        "ConvertBf16ToUInt16Buffers.cpp",
        "ConvertToDestinationPassingStylePass.cpp",
        "DecomposeAffineOpsPass.cpp",
        "DecomposeBatchMmt4DOps.cpp",
        "DecomposeConvolutionToLowerDimOps.cpp",
        "DecomposeLinalgGeneric.cpp",
        "DecomposePackUnPackOps.cpp",
        "DecomposeSoftmax.cpp",
        "EmulateNarrowType.cpp",
        "EncodingUtils.cpp",
        "EraseDeadAllocAndStores.cpp",
        "EraseHALDescriptorTypeFromMemRef.cpp",
        "ExtractAddressComputation.cpp",
        "FlattenMemRefSubspanPass.cpp",
        "FoldAffineMinInDistributedLoops.cpp",
        "FoldTensorExtractOpPass.cpp",
        "FoldTensorSubsetIntoVectorTransferOps.cpp",
        "ForOpCanonicalizationPass.cpp",
        "FuseTensorPadWithConsumer.cpp",
        "GenericVectorization.cpp",
        "HoistRedundantVectorTransfers.cpp",
        "HoistStaticallyBoundAllocations.cpp",
        "HoistUnrolledVectorExtractInsertSlice.cpp",
        "IREEComprehensiveBufferizePass.cpp",
        "IREEExpandStridedMetadata.cpp",
        "InstrumentMemoryAccesses.cpp",
        "LowerUKernelsToCalls.cpp",
        "MaterializeEncodingIntoNop.cpp",
        "MaterializeEncodingIntoPackUnPack.cpp",
        "MemrefCopyToLinalg.cpp",
        "OptimizeVectorTransferPass.cpp",
        "PadDynamicAlloc.cpp",
        "Passes.cpp",
        "PolynomialApproximationPass.cpp",
        "RematerializeParallelOps.cpp",
        "RemoveTrivialLoops.cpp",
        "ReplaceSlowMinMaxOps.cpp",
        "SplitFullPartialTransferPass.cpp",
        "TestExecutablePreprocessing.cpp",
        "TestPartitionableLoopsInterface.cpp",
        "TileAndDistributeToWorkgroupsPass.cpp",
        "TileDispatchUsingInterface.cpp",
        "TileSizeSelection.cpp",
        "TypePropagationPass.cpp",
        "UserConfig.cpp",
        "VectorLayoutAnalysis.cpp",
        "VectorizePad.cpp",
    ],
    hdrs = [
        "BufferizationAnalysis.h",
        "EncodingUtils.h",
        "ExtractAddressComputation.h",
        "Passes.h",
        "TileSizeSelection.h",
        "Transforms.h",
        "UserConfig.h",
        "VectorLayoutAnalysis.h",
    ],
    deps = [
        ":PassHeaders",
        ":PassesIncGen",
        "//compiler/src/iree/compiler/Codegen/Common:FoldTensorExtractOpIncGen",
        "//compiler/src/iree/compiler/Codegen/Dialect/Codegen/IR:IREECodegenDialect",
        "//compiler/src/iree/compiler/Codegen/Interfaces:BufferizationInterfaces",
        "//compiler/src/iree/compiler/Codegen/Interfaces:PartitionableLoopsInterface",
        "//compiler/src/iree/compiler/Codegen/Interfaces:UKernelOpInterface",
        "//compiler/src/iree/compiler/Codegen/Transforms",
        "//compiler/src/iree/compiler/Codegen/Utils",
        "//compiler/src/iree/compiler/Dialect/Flow/IR",
        "//compiler/src/iree/compiler/Dialect/HAL/IR",
        "//compiler/src/iree/compiler/Dialect/Util/IR",
        "//compiler/src/iree/compiler/Utils",
        "//llvm-external-projects/iree-dialects:IREELinalgExtDialect",
        "//llvm-external-projects/iree-dialects:IREELinalgExtTransforms",
        "//llvm-external-projects/iree-dialects:IREEVectorExtDialect",
        "@llvm-project//llvm:Support",
        "@llvm-project//mlir:AffineAnalysis",
        "@llvm-project//mlir:AffineDialect",
        "@llvm-project//mlir:AffineTransforms",
        "@llvm-project//mlir:AffineUtils",
        "@llvm-project//mlir:Analysis",
        "@llvm-project//mlir:ArithDialect",
        "@llvm-project//mlir:ArithTransforms",
        "@llvm-project//mlir:ArithUtils",
        "@llvm-project//mlir:BufferizationDialect",
        "@llvm-project//mlir:BufferizationTransforms",
        "@llvm-project//mlir:DialectUtils",
        "@llvm-project//mlir:FuncDialect",
        "@llvm-project//mlir:FuncTransforms",
        "@llvm-project//mlir:GPUDialect",
        "@llvm-project//mlir:IR",
        "@llvm-project//mlir:LLVMCommonConversion",
        "@llvm-project//mlir:LLVMDialect",
        "@llvm-project//mlir:LinalgDialect",
        "@llvm-project//mlir:LinalgTransforms",
        "@llvm-project//mlir:LinalgUtils",
        "@llvm-project//mlir:LoopLikeInterface",
        "@llvm-project//mlir:MathDialect",
        "@llvm-project//mlir:MathTransforms",
        "@llvm-project//mlir:MemRefDialect",
        "@llvm-project//mlir:MemRefTransforms",
        "@llvm-project//mlir:MemRefUtils",
        "@llvm-project//mlir:Pass",
        "@llvm-project//mlir:SCFDialect",
        "@llvm-project//mlir:SCFToControlFlow",
        "@llvm-project//mlir:SCFTransforms",
        "@llvm-project//mlir:SCFUtils",
        "@llvm-project//mlir:SideEffectInterfaces",
        "@llvm-project//mlir:Support",
        "@llvm-project//mlir:TensorDialect",
        "@llvm-project//mlir:TensorTransforms",
        "@llvm-project//mlir:TensorUtils",
        "@llvm-project//mlir:TilingInterface",
        "@llvm-project//mlir:TransformDialect",
        "@llvm-project//mlir:Transforms",
        "@llvm-project//mlir:ValueBoundsOpInterface",
        "@llvm-project//mlir:VectorDialect",
        "@llvm-project//mlir:VectorTransforms",
        "@llvm-project//mlir:ViewLikeInterface",
    ],
)

# TODO: If the layering causes concerns then the transform dialect interpreter
# should be one level above everything: it is a mechanism by which
# transformations are applied to any IR and needs to register all the dialects
# that may be produced.
# In particular, a single IREE-side transform interpreter is enough to perform
# all kind of transformations and not just codegen.
# This is an opportunity to retire the specific interpreter that is used for
# creating dispatch regions with the transform dialect, but only once the
# layering is correct.
iree_compiler_cc_library(
    name = "TransformDialectInterpreterPass",
    srcs = [
        "CommonDialectRegistration.cpp",
        "MaterializeUserConfigs.cpp",
        "TransformDialectInterpreterPass.cpp",
    ],
    deps = [
        ":Common",
        ":PassHeaders",
        ":PassesIncGen",
        # Dialects
        "//compiler/src/iree/compiler/Codegen/Dialect/Codegen/IR:IREECodegenDialect",
        "//compiler/src/iree/compiler/Dialect/Flow/IR",
        "//llvm-external-projects/iree-dialects:IREELinalgExtDialect",
        "//llvm-external-projects/iree-dialects:IREELinalgExtTransformOps",
        "//llvm-external-projects/iree-dialects:IREELinalgTransformDialect",
        "//llvm-external-projects/iree-dialects:IREEVectorExtDialect",
        "@llvm-project//mlir:AffineDialect",
        "@llvm-project//mlir:AffineUtils",
        "@llvm-project//mlir:ArmSMEDialect",
        "@llvm-project//mlir:AsyncDialect",
        "@llvm-project//mlir:ArithDialect",
        "@llvm-project//mlir:ArithUtils",
        "@llvm-project//mlir:BufferizationDialect",
        "@llvm-project//mlir:BufferizationTransforms",
        "@llvm-project//mlir:FuncDialect",
        "@llvm-project//mlir:GPUDialect",
        "@llvm-project//mlir:LinalgDialect",
        "@llvm-project//mlir:LLVMDialect",
        "@llvm-project//mlir:PDLDialect",
        "@llvm-project//mlir:PDLInterpDialect",
        "@llvm-project//mlir:SCFDialect",
        "@llvm-project//mlir:SCFUtils",
        "@llvm-project//mlir:TensorDialect",
        "@llvm-project//mlir:TransformDialect",
        "@llvm-project//mlir:TransformDialectTransforms",
        "@llvm-project//mlir:VectorDialect",
        # IR
        "@llvm-project//mlir:Analysis",
        "@llvm-project//mlir:IR",
        "@llvm-project//mlir:Pass",
        "@llvm-project//mlir:Rewrite",
        # Interfaces
        # Transforms (needed mostly for the BufferizableOpInterfaceImpl)
        "@llvm-project//mlir:ArithTransforms",
        "@llvm-project//mlir:LinalgTransforms",
        "@llvm-project//mlir:SCFTransforms",
        "@llvm-project//mlir:TensorTransforms",
        "@llvm-project//mlir:Transforms",
        "@llvm-project//mlir:VectorTransforms",
        # Other Stuff
        "//compiler/src/iree/compiler/Utils",
        "@llvm-project//llvm:Support",
        "@llvm-project//mlir:Support",
        "@llvm-project//mlir:DialectUtils",
        # TransformStrategies
        "//compiler/src/iree/compiler/Codegen/TransformStrategies/Common:TransformStrategies",
        # TransformExtensions (needed for registration in the pass)
        "//llvm-external-projects/iree-dialects:IREEDialectsTransforms",
        "//compiler/src/iree/compiler/Codegen/Common/TransformExtensions:CommonExtensions",
        "//compiler/src/iree/compiler/Dialect/Flow/TransformExtensions:FlowExtensions",
        "//compiler/src/iree/compiler/Codegen/LLVMCPU/TransformExtensions:LLVMCPUExtensions",
        "//compiler/src/iree/compiler/Codegen/LLVMGPU/TransformExtensions:LLVMGPUExtensions",
        "@llvm-project//mlir:AffineTransformOps",
        "@llvm-project//mlir:BufferizationTransformOps",
        "@llvm-project//mlir:GPUTransformOps",
        "@llvm-project//mlir:LinalgTransformOps",
        "@llvm-project//mlir:MemRefTransformOps",
        "@llvm-project//mlir:SCFTransformOps",
        "@llvm-project//mlir:TensorTransformOps",
        "@llvm-project//mlir:TransformLoopExtension",
        "@llvm-project//mlir:VectorTransformOps",
    ],
)
